﻿Imports BUS
Imports DTO

Public Class frmTraSach : Inherits DevComponents.DotNetBar.Office2007Form

    Dim dg As New CDocGiaDTO()
    Private Function KiemTra() As Boolean
        If (txtMaDocGia.Text = "") Then
            MessageBox.Show("Chưa nhập mã độc giả.", "Thông báo")
            Return False
        End If

        dg = New CDocGiaBUS().LayThongTinDocGiaKhiBietMa(Convert.ToInt32(txtMaDocGia.Text))
        If dg Is Nothing Then
            MessageBox.Show("Mã độc giả không hợp lệ", "Thông báo")
            Return False
        End If
        Return True
    End Function

    Private Sub btnTimkiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimkiem.Click
        If (Not KiemTra()) Then
            Return
        End If
        txtTenDocGia.Text = dg.HoTen
        txtTienNo.Text = dg.TongNo

        Dim lctpm As New List(Of CChiTietPhieuMuonDTO)
        Dim stt As Integer = 0
        Dim songay As Integer
        Dim ngayvuotqua As Integer = New CThamSoBUS().LayThoiHanMuonToiDa()
        Dim tienphat As Integer = New CThamSoBUS().LayTienPhatTraTre()
        Dim tiendephat As Integer = 0
        lctpm = New CChiTiepPhieuMuonBUS().LayChiTietPhieuMuonTheoMa(Convert.ToInt32(txtMaDocGia.Text))
        DataGridViewChiTiet.Rows.Clear()
        stt = 0
        For Each lc As CChiTietPhieuMuonDTO In lctpm
            stt += 1

            songay = DateDiff(DateInterval.Day, lc.NgayMuon, DateTime.Now, FirstDayOfWeek.Monday)
            If songay > ngayvuotqua Then
                tiendephat = tienphat * (songay - ngayvuotqua)
            Else
                tiendephat = 0
            End If
            DataGridViewChiTiet.Rows.Add(stt, lc.MaPhieuMuon, lc.Ma, lc.MaSach, lc.NgayMuon, songay, tiendephat, False)
        Next
        txtTienPhat.Text = 0
        txtTongNo.Text = txtTienNo.Text
    End Sub

    Private Sub frmTraSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpNgayTra.Value = DateTime.Now
    End Sub

    Private Sub btnTra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTra.Click
        If (Not KiemTra()) Then
            Return
        End If
        Dim tien As Integer = 0
        Dim check As Integer = 0

        For Each dgr As DataGridViewRow In DataGridViewChiTiet.Rows
            If dgr.Cells("clTinhTrang").Value Then
                tien += dgr.Cells("clTienPhat").Value
                check += 1


            End If
        Next
        If check = 0 Then
            MessageBox.Show("Chưa chọn sách trả", "Thông báo")
            Return
        End If
        txtTienPhat.Text = tien.ToString()
        txtTongNo.Text = (Convert.ToInt32(txtTienNo.Text) + tien).ToString()


        Dim phieutra As New CPhieuTraDTO()
        phieutra.MaDocGia = txtMaDocGia.Text
        phieutra.NgayTra = dtpNgayTra.Value
        phieutra.TienNo = txtTienNo.Text
        phieutra.TienPhatKyNay = txtTienPhat.Text
        phieutra.TongNo = txtTongNo.Text

        Dim ttg As Integer = New CPhieuTraBUS().ThemPhieuTra(phieutra)
        If (ttg) Then
            Dim ctpt As CChiTietPhieuTraDTO
            Dim ctpbus As New CChiTietPhieuTraBUS()
            For Each dgr As DataGridViewRow In DataGridViewChiTiet.Rows
                If dgr.Cells("clTinhTrang").Value Then
                    ctpt = New CChiTietPhieuTraDTO()
                    ctpt.MaChiTietPhieuMuon = dgr.Cells("clMaChiTietPhieuMuon").Value
                    ctpt.MaPhieuMuon = dgr.Cells("clMaPhieuMuon").Value
                    ctpt.MaPhieuTra = phieutra.Ma
                    ctpt.MaSach = dgr.Cells("clMaSach").Value
                    ctpt.NgayMuon = dgr.Cells("clNgayMuon").Value
                    ctpt.SoNgayMuon = dgr.Cells("clSoNgayMuon").Value
                    ctpt.TienPhat = dgr.Cells("clTienPhat").Value
                    ctpbus.ThemChiTietPhieuTra(ctpt)
                End If
            Next
        End If
        MessageBox.Show("Trả sách thành công", "Thông báo")
        btnTimkiem.PerformClick()
       
    End Sub

    Private Sub DataGridViewChiTiet_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewChiTiet.CurrentCellDirtyStateChanged
        If TypeOf DataGridViewChiTiet.CurrentCell Is DataGridViewCheckBoxCell Then
            DataGridViewChiTiet.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
    End Sub

    Private Sub DataGridViewChiTiet_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewChiTiet.CellValueChanged
        Dim tien As Integer = 0
        If e.RowIndex > -1 Then
            For Each dgr As DataGridViewRow In DataGridViewChiTiet.Rows
                If dgr.Cells("clTinhTrang").Value Then
                    tien += Convert.ToInt32(dgr.Cells("clTienPhat").Value)
                End If
            Next
            txtTienPhat.Text = tien.ToString()
            txtTongNo.Text = (Convert.ToInt32(txtTienNo.Text) + tien).ToString()
        End If
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        txtMaDocGia.Text = ""
        txtTenDocGia.Text = ""
        txtTienNo.Text = ""
        txtTienPhat.Text = ""
        txtTongNo.Text = ""
        DataGridViewChiTiet.Rows.Clear()
    End Sub
End Class